package com.tangtang.core.basic;

import java.util.List;

import com.tangtang.core.StringUtil;

public class PagedQuery<T> {
    private String tableName;
    private String where = "";

    private String order = "";
    private String columns = " * ";
    private Integer pageIndex;
    private Integer limit;
    private Integer recordCount;
    private List<T> result;
    private Class<T> entityClass;

    public String getWhere() {
        return where;
    }

    public void setWhere(String where) {
        this.where = where;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public String getColumns() {
        return columns;
    }

    public void setColumns(String columns) {
        this.columns = columns;
    }

    public String getTableName() {
        if (StringUtil.isBlank(tableName)) {
            tableName = entityClass.getSimpleName();
        }
        return tableName;
    }

    /**
     * 表名
     *
     * @param tableName
     */
    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    /**
     * 默认10条
     *
     * @param pageIndex
     */
    public PagedQuery(Integer pageIndex, Class<T> entityClass) {
        this(pageIndex, 10, entityClass);
    }

    /**
     * @param pageIndex
     */
    public PagedQuery(Integer pageIndex, Integer limit, Class<T> entityClass) {
        if (pageIndex == null || pageIndex <= 0)
            pageIndex = 1;
        if (limit == null || limit <= 0)
            limit = 10;
        this.pageIndex = pageIndex;
        this.limit = limit;
        this.entityClass = entityClass;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public Integer getLimit() {
        return limit;
    }

    public Integer getRecordCount() {
        return recordCount;
    }

    public void setRecordCount(int recordCount) {
        this.recordCount = recordCount;
    }

    public Integer getPageCount() {
        if (recordCount <= 0)
            return 0;
        return (int) Math.ceil((double) recordCount / (double) limit);
    }

    public List<T> getResult() {
        return result;
    }

    public void setResult(List<T> tmp) {
        result = tmp;
    }

    public Integer getOffset() {
        return (pageIndex - 1) * limit;
    }
}
